package com.zhny.aiot.rain.snow.plugin;

import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.modbus.ModbusNumDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemRequestMessage;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.TaskEntity;
import com.zhny.aiot.rain.snow.plugin.model.RainSnowStoreEntry;

public class RainSnowDevice extends ModbusNumDevice<RainSnowStoreEntry> {


    public RainSnowDevice(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super (engine, factory, centerFacilities,entity);
    }

    @Override
    public RainSnowStoreEntry decode(ModemResponseMessage modemResponseMessage) {
        RainSnowStoreEntry entry = new RainSnowStoreEntry ();
        int status = modemResponseMessage.getData ().readShort ();
        if(status == 0)
            entry.setStatus ("正常");
        else
            entry.setStatus ("报警");
        return entry;
    }

    @Override
    public boolean checkError(ModemResponseMessage modemResponseMessage) {
        return modemResponseMessage.getData ().readableBytes () == 2;
    }

    @Override
    protected ModemRequestMessage getCommand(TaskEntity taskEntity) {
        return getFactory ().getRequestMessage (getConfig ());
    }

    @Override
    protected void disposable() {

    }
}
